package com.myl.generator.controller;

import com.myl.core.utils.ResponseResult;
import com.myl.generator.service.impl.DatabaseServiceImpl;
import com.myl.generator.service.GeneratorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * GeneratorController
 * author: myl <ixiaomu@qq.com> 2023/12/26
 * tips:
 */
@Tag(name = "工具" )
@Slf4j
@RestController
public class GeneratorController {

    @Autowired
    private DatabaseServiceImpl databaseService;

    @Autowired
    private GeneratorService generatorService;

    @Operation(summary = "获取所有表" , method = "GET" )
    @GetMapping("/tool/all-tables" )
    public ResponseResult<?> getAllTable() {
        Map<String, Object> data = databaseService.getAllTable();
        return ResponseResult.success("获取数据成功" , data);
    }

    @Operation(summary = "获取表字段信息" , method = "GET" )
    @GetMapping("/tool/table-columns" )
    public ResponseResult<?> getTableColumns(
            @Parameter(description = "表名") @RequestParam(required = true, name = "tableName") String tableName
    ) {
        log.info("PARAMS: {}", tableName);
        Map<String, Object> data = databaseService.getTableColumns(tableName);
        return ResponseResult.success("获取数据成功" , data);
    }

    /**
     * 代码生成
     *
     * @return ResponseResult
     */
    @Operation(summary = "代码生成器" , method = "GET" )
    @GetMapping("/tool/generator" )
    public ResponseResult<?> generator(
        @Parameter(description = "表名, 多个用逗号隔开") @RequestParam(name = "tableNames") String... tableNames
    ) {
        for (String tableName : tableNames) {
            log.info("TableName: {}", tableName);
        }

//        GeneratorService service = new GeneratorService();
        generatorService.doGenerator(tableNames);
        return ResponseResult.success("操作成功");
    }
}
